/*     Copyright (c) 2018, jymenu.com. All rights reserved.   */
package com.yunhaikeji.core.web;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * ProcessResult
 *
 * @Author : zhuo
 * @Date : 2018/7/18 19:54
 * @Description :
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ProcessResult<T> extends LinkedHashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = 2987833679655696948L;

    public ProcessResult() {
        super();
        this.put("success", true);
        this.put("code", "0");
        this.put("msg", "操作成功");
    }

    public ProcessResult(boolean success, String code, String msg) {
        super();
        this.put("success", success);
        this.put("code", code);
        this.put("msg", msg);
    }

    public boolean isSuccess() {
        return (boolean) this.get("success");
    }

    public ProcessResult setSuccess(boolean success) {
        this.put("success", success);
        return this;
    }

    public String getCode() {
        return (String) this.get("code");
    }

    public ProcessResult setCode(String code) {
        this.put("code", code);
        return this;
    }

    public String getMsg() {
        return (String) this.get("msg");
    }

    public ProcessResult setMsg(String msg) {
        this.put("msg", msg);
        return this;
    }

    @Override
    public ProcessResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public ProcessResult remove(String key) {
        super.remove(key);
        return this;
    }

    public static ProcessResult success() {
        return new ProcessResult();
    }

    public static <T> ProcessResult success(T value) {
        ProcessResult processResult = new ProcessResult();
        processResult.put("data", value);
        return processResult;
    }

    public static <T> ProcessResult success(String key, T value) {
        ProcessResult processResult = new ProcessResult();
        processResult.put(key, value);
        return processResult;
    }

    public static ProcessResult error() {
        ProcessResult processResult = new ProcessResult();
        processResult.setSuccess(false);
        processResult.setCode("-1");
        return processResult;
    }

    public static ProcessResult error(String msg) {
        ProcessResult processResult = error();
        processResult.setMsg(msg);
        return processResult;
    }

}
